package com.example.administrator.clock;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2018/4/1.
 */

public class MyView extends View {
    private Paint paint = new Paint();
    private int hradius = 60;
    private int mradius = 70;
    private int sradius = 80;

    private float hour;
    private int minute;
    private int second;

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    getNowTime();
                    postInvalidate();
                }
            }


        }).start();
    }

    private void getNowTime() {
        Time time = new Time();
        time.setToNow();
        int h =time.hour;
        minute = time.minute;
        second = time.second;
        hour = h+minute*1.0f/60;
    }


    private void initPaint() {
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        canvas.drawCircle(200, 200, 100, paint);

        float x = (float) (200 + hradius *Math.sin(hour * 30.0 /180 * Math.PI));
        float y = (float) ((200 - hradius *Math.cos(hour * 30.0 /180 * Math.PI)));
        paint.setColor(Color.RED);
        canvas.drawLine(200, 200, x, y, paint);

        float mx = (float) (200 + mradius *Math.sin(minute * 6.0 /180 * Math.PI));
        float my = (float) ((200 - mradius *Math.cos(minute * 6.0 /180 * Math.PI)));
        paint.setColor(Color.GREEN);
        canvas.drawLine(200, 200, mx, my, paint);

        float sx = (float) (200 + sradius *Math.sin(second * 6.0 /180 * Math.PI));
        float sy = (float) ((200 - sradius * Math.cos(second * 6.0 /180 * Math.PI)));
        paint.setColor(Color.BLUE);
        canvas.drawLine(200, 200, sx, sy, paint);


        super.onDraw(canvas);
    }
}
